﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ct_Aliyun
{
    public class OssCenter
    {
        private string AccessKeyId = "LTAI5t87LnK6rSfhxbePUzTW";

        private string AccessKeySecret = "3gMXCjnDmGQvuDtBUyIQYS6eWgNJ3m";

        private string Endpoint = "oss-cn-beijing.aliyuncs.com";

        public static OssCenter CreateOssCenter(string AccessKeyId, string AccessKeySecret, string Endpoint)
        {
            OssCenter ossCenter = new OssCenter();
            ossCenter.AccessKeyId = AccessKeyId;
            ossCenter.AccessKeySecret = AccessKeySecret;
            ossCenter.Endpoint = Endpoint;

            return ossCenter;
        }



        #region 下载


        /// <summary>
        /// 同步下载
        /// </summary>
        /// <param name="bucketName">对象同名</param>
        /// <param name="OssFilePath">Oss远程路径</param>
        /// <param name="DownloadLocalDir">下载路径</param>
        /// <param name="Timeout">请求Timeout</param>
        /// <param name="SaveFileName">保存的名字</param>
        public bool Download(string bucketName, string OssFilePath, string DownloadLocalDir, int Timeout = -1, string SaveFileName = "")
        {
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint);

            return aliyun_SDK.DownloadObject(bucketName, OssFilePath, DownloadLocalDir, Timeout, SaveFileName);

        }



        public OssTaskRequest DownloadAsync(string bucketName, string OssFilePath, string DownloadLocalDir, Action<RequestState> Callback, int Timeout = -1, string SaveFileName = "")
        {
            OssTaskRequest ossTaskBackInfo = new OssTaskRequest();
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint, ossTaskBackInfo);
            Task.Run(() => {
                DownloadWork(aliyun_SDK, bucketName, OssFilePath, DownloadLocalDir, Callback, ossTaskBackInfo, Timeout, SaveFileName);

            });
            return ossTaskBackInfo;



        }


        private async void DownloadWork(Aliyun_SDK aliyun_SDK, string bucketName, string OssFilePath, string DownloadLocalDir, Action<RequestState> Callback, OssTaskRequest ossTaskBackInfo, int Timeout = -1, string SaveFileName = "")
        {

            bool res = await aliyun_SDK.DownloadObjectWithProgress(bucketName, OssFilePath, DownloadLocalDir, Timeout, SaveFileName);
            if (res)
            {
                ossTaskBackInfo.requestState = RequestState.Sucess;
                Callback(RequestState.Sucess);
            }
            else
            {
                ossTaskBackInfo.requestState = RequestState.Faile;
                Callback(RequestState.Faile);
            }
        }









        public byte[] DownLaodObjectRawData(string bucketName, string OssFilePath, int Timeout = -1)
        {
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint);

            return aliyun_SDK.DownLaodObjectRawData(bucketName, OssFilePath, Timeout);

        }


        public OssTaskRequest DownloadObjectRawDataAsync(string bucketName, string OssFilePath, Action<RequestState> Callback, int Timeout = -1)
        {
            OssTaskRequest ossTaskBackInfo = new OssTaskRequest();
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint, ossTaskBackInfo);
            Task.Run(() => {
                Task<byte[]> vs = DownloadObjectRawDataWork(aliyun_SDK, bucketName, OssFilePath, Callback, ossTaskBackInfo, Timeout );
                ossTaskBackInfo.content = vs.Result;

            });
            return ossTaskBackInfo;



        }


        private async Task<byte[]> DownloadObjectRawDataWork(Aliyun_SDK aliyun_SDK, string bucketName, string OssFilePath, Action<RequestState> Callback, OssTaskRequest ossTaskBackInfo, int Timeout = -1 )
        {

            byte[] res = await aliyun_SDK.DownLaodObjectRawDatatWithProgress(bucketName, OssFilePath, Timeout);
            if (res != null)
            {
                ossTaskBackInfo.requestState = RequestState.Sucess;
                Callback(RequestState.Sucess);
            }
            else
            {
                ossTaskBackInfo.requestState = RequestState.Faile;
                Callback(RequestState.Faile);
            }
                
                
    
            return res;
        }
        #endregion




        #region 上传

        public bool Upload(string BucketName, string LocalFilePath, string OssFileDir, int Timeout = -1, string SaveFileName = "")
        {
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint);

            return aliyun_SDK.UploadObject(BucketName, LocalFilePath, OssFileDir, Timeout, SaveFileName);

        }


        public OssTaskRequest UploadAsync(string BucketName, string LocalFilePath, string OssFileDir, Action<RequestState> Callback, int Timeout = -1, string SaveFileName = "")
        {
            OssTaskRequest ossTaskBackInfo = new OssTaskRequest();
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint, ossTaskBackInfo);

            Task.Run(() => {

                UploadWork(aliyun_SDK, BucketName, LocalFilePath, OssFileDir, Callback, ossTaskBackInfo, Timeout, SaveFileName);
            });
            return ossTaskBackInfo;



        }


        private async void UploadWork(Aliyun_SDK aliyun_SDK, string BucketName, string LocalFilePath, string OssFileDir, Action<RequestState> Callback, OssTaskRequest ossTaskBackInfo, int Timeout = -1, string SaveFileName = "")
        {
            bool res = await aliyun_SDK.UploadObjectWithProgress(BucketName, LocalFilePath, OssFileDir, Timeout, SaveFileName);
            if (res)
            {
                ossTaskBackInfo.requestState = RequestState.Sucess;
                Callback(RequestState.Sucess);
            }
            else
            {
                ossTaskBackInfo.requestState = RequestState.Faile;
                Callback(RequestState.Faile);
            }
        }





        public bool UploadRawData(string BucketName, byte[] Data, string OssFileDir, int Timeout = -1, string SaveFileName = "")
        {
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint);

            return aliyun_SDK.UploadObjectRawData(BucketName, Data, OssFileDir, Timeout, SaveFileName);



        }



        public OssTaskRequest UploadRawDataAsync(string BucketName, byte[] Data, string OssFileDir, Action<RequestState> Callback, int Timeout = -1, string SaveFileName = "")
        {
            OssTaskRequest ossTaskBackInfo = new OssTaskRequest();
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint, ossTaskBackInfo);

            Task.Run(() => {

                UploadRawDataWork(aliyun_SDK, BucketName, Data, OssFileDir, Callback, ossTaskBackInfo, Timeout, SaveFileName);
            });
            return ossTaskBackInfo;


        }

        private async void UploadRawDataWork(Aliyun_SDK aliyun_SDK, string BucketName, byte[] Data, string OssFileDir, Action<RequestState> Callback, OssTaskRequest ossTaskBackInfo, int Timeout = -1, string SaveFileName = "")
        {
            bool res = await aliyun_SDK.UploadObjectRawDateaWithProgress(BucketName, Data, OssFileDir, Timeout, SaveFileName);
            if (res)
            {
                ossTaskBackInfo.requestState = RequestState.Sucess;
                Callback(RequestState.Sucess);
            }
            else
            {
                ossTaskBackInfo.requestState = RequestState.Faile;
                Callback(RequestState.Faile);
            }
        }









        public OssTaskRequest UploadMultipartAsync(string BucketName, string LocalFilePath, string OssFileDir, Action<RequestState> Callback, int Timeout = -1, string SaveFileName = "")
        {
            OssTaskRequest ossTaskBackInfo = new OssTaskRequest();
            Aliyun_SDK aliyun_SDK = new Aliyun_SDK(AccessKeyId, AccessKeySecret, Endpoint, ossTaskBackInfo);
            Task.Run(() => {
                UploadMultipartWork(aliyun_SDK, BucketName, LocalFilePath, OssFileDir, Callback, Timeout, SaveFileName);
            });

            return ossTaskBackInfo;


        }


        private async void UploadMultipartWork(Aliyun_SDK aliyun_SDK, string BucketName, string LocalFilePath, string OssFileDir, Action<RequestState> Callback, int Timeout = -1, string SaveFileName = "")
        {
            bool res = await aliyun_SDK.UploadMultipartWithProgress(BucketName, LocalFilePath, OssFileDir, Timeout, SaveFileName);
            if (res) Callback(RequestState.Sucess);
            else Callback(RequestState.Faile);


        }
        #endregion
    }
}
